package com.daogu.fuelcard.controller;

import com.daogu.fuelcard.entity.User;
import com.daogu.fuelcard.service.UserService;
import com.daogu.fuelcard.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author daogu
 */
@Controller
@Tag(name = "用户控制器")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(description = "获取用户信息")
    @GetMapping("/getUserList")
    @ResponseBody
    public Result<PageInfo<User>> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.getUserList(pageSize,pageNum);
    }

    @Operation(description = "新增用户")
    @PostMapping("/insertUser")
    @ResponseBody
    public Result<Void> insertUser(@RequestBody @Validated User user) {
        userService.insertUser(user);
        return Result.accept();
    }

    @Operation(description = "登录")
    @PostMapping("/checkLogin")
    public String checkLogin(@RequestParam("username") String userName, @RequestParam("password") String password, HttpServletResponse response) {
        if (userService.checkLogin(userName,password,response)) {
            return "index";
        } else {
            return "login";
        }

    }

    @Operation(description = "登录页面")
    @GetMapping("/login")
    public String login() {
        return "login";
    }
}
